import Vue from "vue";
import Router from "vue-router";
// import Home from "./views/Home.vue";
import Login from "./views/Login.vue";
import Home from "./views/Home.vue";
import Welcome from "./views/Welcome.vue";
import User from "./views/user/User.vue";
import Rights from "./views/auth/Rights.vue";
import Roles from "./views/auth/Roles.vue";

Vue.use(Router);

var router = new Router({
  routes: [
    { path: "/login", component: Login },
    { path: "/", redirect: "/welcome" },
    {
      path: "/home",
      component: Home,

      redirect: "/welcome",
      children: [
        { path: "/welcome", component: Welcome },
        { path: "/users", component: User },

        { path: "/rights", component: Rights },
        { path: "/roles", component: Roles }
      ]
    }
  ]
});

// 路由守卫
router.beforeEach(function(to, from, next) {
  // 获取token
  var token = window.sessionStorage.getItem("token");

  if (token === null && to.path !== "/login") {
    return next("/login");
  }
  next();
});

export default router;
